﻿using NoteBook_Product.DialogsService;
using NoteBook_Product.Models;
using NoteBook_Product.Services;
using NoteBook_Shared.Dtos;
using NoteBook_WebApi.Context;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;

namespace NoteBook_Product.ViewModels
{
    public class IndexViewModel : NavigationViewModel
    {
        private readonly IDialogHostService _dialogService;

        private readonly IToDoService _toDoService;
        private readonly IMemoService _memoService;

        public IndexViewModel(IContainerProvider provider, IDialogHostService dialogService) : base(provider)
        {
            _dialogService = dialogService;

            _toDoService = provider.Resolve<IToDoService>();
            _memoService = provider.Resolve<IMemoService>();

            TaskBars = new();
            CreateTaskBar();
            ExecutCommand = new(Execute);
            EditToDoCommand = new(AddToDo);
            EditMemoCommand = new(AddMemo);
            ToDoComplatedCommand = new(Complated);

        }

        private async void Complated(ToDoDto dto)
        {
            var updateResult = await _toDoService.UpdateAsync(dto);
            if (updateResult.Status)
            {
                var todo = ToDoDtos.FirstOrDefault(x => x.Id == dto.Id);
                if (todo != null)
                {
                    ToDoDtos.Remove(todo);
                }
            }
        }

        #region 属性

        public ObservableCollection<TaskBar> TaskBars { get; set; }

        public ObservableCollection<ToDoDto> ToDoDtos { get; set; } = new();

        public ObservableCollection<MemoDto> MemoDtos { get; set; } = new();

        #endregion

        #region 命令

        public DelegateCommand<string> ExecutCommand { get; set; }

        public DelegateCommand<ToDoDto> EditToDoCommand { get; set; }

        public DelegateCommand<MemoDto> EditMemoCommand { get; set; }

        public DelegateCommand<ToDoDto> ToDoComplatedCommand { get; set; }

        #endregion


        private void CreateTaskBar()
        {
            TaskBars.Add(new("ClockFast", "汇总", "9", "#FF0CA0FF", ""));
            TaskBars.Add(new("ClockCheckOutline", "已完成", "9", "#FF1ECA3A", ""));
            TaskBars.Add(new("ChartLineVariant", "完成比例", "100%", "#FF02C6DC", ""));
            TaskBars.Add(new("PlaylistStar", "备忘录", "19", "#FFFFA000", ""));
        }

        private void Execute(string obj)
        {
            switch (obj)
            {
                case "新增待办": AddToDo(); return;
                case "新增备忘录": AddMemo(); return;
            }
        }

        private async void AddToDo(ToDoDto model = null)
        {
            DialogParameters param = new();
            if (model != null) param.Add("Value", model);

            var dialogResult = await _dialogService.ShowDialog("AddToDoView", param);
            if (dialogResult.Result != ButtonResult.OK) return;
            var todo = dialogResult.Parameters.GetValue<ToDoDto>("Value");
            if (todo.Id > 0)
            {
                var updateResult = await _toDoService.UpdateAsync(model);
                if (updateResult.Status)
                {
                    var todoModel = ToDoDtos.FirstOrDefault(x => x.Id.Equals(todo.Id));
                    if (todoModel != null)
                    {
                        todoModel = model;
                    }
                }
            }
            else
            {
                var addResult = await _toDoService.AddAsync(todo);
                if (addResult.Status)
                {
                    ToDoDtos.Add(addResult.Result);
                }
            }
        }

        private async void AddMemo(MemoDto model = null)
        {
            DialogParameters param = new();
            if (model != null) param.Add("Value", value: model);

            var dialogResult = await _dialogService.ShowDialog("AddMemoView", param);
            if (dialogResult.Result != ButtonResult.OK) return;
            var memo = dialogResult.Parameters.GetValue<MemoDto>("Value");
            if (memo.Id > 0)
            {
                var updateResult = await _memoService.UpdateAsync(model);
                if (updateResult.Status)
                {
                    var memoModel = MemoDtos.FirstOrDefault(x => x.Id.Equals(memo.Id));
                    if (memoModel != null)
                    {
                        memoModel = model;
                    }
                }
            }
            else
            {
                var memoResult = await _memoService.AddAsync(memo);
                if (memoResult.Status)
                {
                    MemoDtos.Add(memoResult.Result);
                }
            }
        }
    }
}
